package com.itheima.interceptor;

import com.alibaba.fastjson.JSON;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.util.JedisUtils;
import com.qiniu.util.Json;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author liam
 * @description 登录拦截器
 * @date 2020/3/3-19:31
 * @Version 1.0.0
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    private JedisUtils jedisUtils;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.取得参数中的token
        String token = request.getHeader("token");
//        此处token是否需要判空？
        if (token == null|| token ==""||token=="null") {
            doResponse(request,response);
            return false;
        }
        //2.根据token查询对应用户
        String memberStr = jedisUtils.get(token);
        if (memberStr==null) {
            doResponse(request,response);
            return false;
        }

        return true;
    }

    public void doResponse(HttpServletRequest request,HttpServletResponse response) throws IOException {
        HashMap<String, String> map = new HashMap<>();
        Result result = Result.error("请先登录!");
        result.setData(401);
        String res = JSON.toJSONString(result);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().print(res);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
